Imports MIMS.WRS.Entities
Imports MIMS.WRS.Business


Public Class DOList
    Inherits System.Web.UI.Page


    Protected WithEvents lblTotalIssued As System.Web.UI.WebControls.Label
    Protected WithEvents txtTotalQtyOrdered As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtTotalQtyCompleted As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtQuantityAvailable As System.Web.UI.WebControls.TextBox
    Dim totalDeliveryQty As Integer = 0

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents rptDO As System.Web.UI.WebControls.Repeater
    Protected WithEvents ValidationSummary1 As System.Web.UI.WebControls.ValidationSummary
    Protected WithEvents btnSaveAndNext As System.Web.UI.WebControls.Button
    Protected WithEvents btnSave As System.Web.UI.WebControls.Button
    Protected WithEvents btnAddDO As System.Web.UI.HtmlControls.HtmlInputButton
    Protected WithEvents hidOrderNo As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents hidSupplierCode As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents hidOrderDate As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents hidIssuedDate As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents hidIssuedQty As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents hidCustomerName As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents hidPOID As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents hidOfficeID As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents rptAQ As System.Web.UI.WebControls.Repeater
    Protected WithEvents btnAddAQ As System.Web.UI.HtmlControls.HtmlInputButton
    Protected WithEvents hidAQComment As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents hidAQAmount As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents hidAQPOID As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents lblMessage As System.Web.UI.WebControls.Label

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Security.DenyIDSUser()

        If Not IsPostBack Then
            lblMessage.Text = ""

            'get total quantity available(completed) for this bulk order
            Dim bo As BulkOrder = BulkOrderService.GetBulkOrderDetails(RequestContext.BulkOrderID)
            Dim completedQty As Integer
            completedQty = bo.TotalQtyCompleted
            'Dim bo As BulkOrder = BulkOrderService.GetBulkOrder(RequestContext.BulkOrderID)
            Dim doCol As DeliveredOrderCollection = DeliveredOrderService.GetDeliveredOrders(RequestContext.BulkOrderID)
            rptDO.DataSource = doCol
            rptDO.DataBind()

            'calculated total delivered quantity
            For Each doObj As DeliveredOrder In doCol
                totalDeliveryQty += doObj.QtyIssued
            Next

            If totalDeliveryQty > completedQty Then
                lblMessage.Text = "You have allocated more stock than there is available, order more stock."
            End If

            lblTotalIssued.Text = totalDeliveryQty.ToString()
            'rptAQ.DataSource = AdjustQuantityService.GetAdjustQuantities(RequestContext.BulkOrderID)
            'rptAQ.DataBind()
            txtTotalQtyOrdered.Text = bo.TotalQtyOrdered.ToString()
            txtTotalQtyCompleted.Text = bo.TotalQtyCompleted.ToString()
            If (bo.QuantityAvailable <> Integer.MinValue) Then
                txtQuantityAvailable.Text = bo.QuantityAvailable.ToString()
            Else
                txtQuantityAvailable.Text = "0"
            End If

        End If
    End Sub

    Private Sub rptDO_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptDO.ItemDataBound
        If (e.Item.ItemType = ListItemType.AlternatingItem _
            OrElse e.Item.ItemType = ListItemType.Item) Then
            Dim calIssuedDate As DateTimePicker = CType(e.Item.FindControl("calDateIssued"), DateTimePicker)
            calIssuedDate.SelectedValue = CType(e.Item.DataItem, DeliveredOrder).DateIssued

            Dim lnkDelete As LinkButton = CType(e.Item.FindControl("lnkDelete"), LinkButton)
            lnkDelete.Attributes("onclick") = "return confirm('Are you sure you want to delete?');"
                        
            'lnkDelete.Attributes("onclick") = "return confirm('Are you sure you want to delete?');"
        End If
    End Sub

    Protected Function GetOrderDateHTML(ByVal Container As Object) As String
        Dim orderDate As Object = DataBinder.Eval(Container, "DataItem.OrderDate")
        If (orderDate Is DBNull.Value OrElse Convert.ToDateTime(orderDate).Equals(Nulls.NullDate)) Then
            Return "&nbsp;"
        Else
            Return Convert.ToDateTime(orderDate).ToString(Config.DateFormat)
        End If
    End Function

    Protected Function GetShippedDateHTML(ByVal Container As Object) As String
        Dim shippedDate As Object = DataBinder.Eval(Container, "DataItem.ShippedDate")
        If (shippedDate Is DBNull.Value OrElse Convert.ToDateTime(shippedDate).Equals(Nulls.NullDate)) Then
            Return "&nbsp;"
        Else
            Return Convert.ToDateTime(shippedDate).ToString(Config.DateFormat)
        End If
    End Function
    Private Sub NavigateNext()
        Dim id As String = RequestContext.BulkOrderID.ToString()
        Dim idx As Integer = RequestContext.SelectedIDs.IndexOf(id)

        ' the last item in RequestContext.SelectedIDs is a empty one
        ' by pass that item (Count - 2)
        If (idx < RequestContext.SelectedIDs.Count - 2) Then
            Response.Redirect(ResolveUrl(UrlBuilder.GetBODetailUrl(RequestContext.SupplierCode, Convert.ToInt32(RequestContext.SelectedIDs(idx + 1)), RequestContext.ChainOfID, False, True, 0)))
        Else
            Response.Redirect(ResolveUrl(UrlBuilder.GetBODetailUrl(RequestContext.SupplierCode, Convert.ToInt32(id), RequestContext.ChainOfID, True, True, 0)))
        End If
    End Sub

    Private Sub Save()
        For Each ri As RepeaterItem In rptDO.Items
            If ri.ItemType = ListItemType.AlternatingItem _
                OrElse ri.ItemType = ListItemType.Item Then
                Dim hidID As HtmlInputHidden = CType(ri.FindControl("hidDOId"), HtmlInputHidden)
                Dim txtQty As TextBox = CType(ri.FindControl("txtQtyIssued"), TextBox)
                'Dim txtCustomerName As TextBox = CType(ri.FindControl("txtCustomerName"), TextBox)
                Dim calDateIssued As DateTimePicker = CType(ri.FindControl("calDateIssued"), DateTimePicker)
                Dim dOrder As DeliveredOrder = DeliveredOrderService.GetDeliveredOrder(Integer.Parse(hidID.Value))
                dOrder.DateIssued = calDateIssued.SelectedValue
                dOrder.QtyIssued = Integer.Parse(txtQty.Text)
                'dOrder.CustomerName = txtCustomerName.Text
                DeliveredOrderService.UpdateDeliveredOrder(dOrder)
            End If
        Next

    End Sub

    Private Sub btnAddDO_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddDO.ServerClick
        Dim proCode As String = BulkOrderService.GetBulkOrder(RequestContext.BulkOrderID).ProductCode
        Dim dOrder As DeliveredOrder = New DeliveredOrder
        dOrder.DateIssued = DateTime.Parse(hidIssuedDate.Value)
        dOrder.OfficeID = Integer.Parse(hidOfficeID.Value)
        dOrder.OrderDate = DateTime.Parse(hidOrderDate.Value)
        dOrder.OrderNo = hidOrderNo.Value.PadLeft(3, "0"c)
        dOrder.ProductCode = proCode
        dOrder.QtyIssued = Integer.Parse(hidIssuedQty.Value)
        dOrder.SupplierCode = hidSupplierCode.Value.PadLeft(3, "0"c)
        dOrder.YY = dOrder.OrderDate.Year.ToString().Substring(2, 2)
        dOrder.BulkOrderID = RequestContext.BulkOrderID
        dOrder.CustomerName = hidCustomerName.Value
        dOrder.POID = Integer.Parse(hidPOID.Value)
        DeliveredOrderService.CreateDeliveredOrder(dOrder)
        Response.Redirect(Request.RawUrl)
    End Sub

    'Private Sub btnAddAQ_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddAQ.ServerClick        
    '    Dim aq As AdjustQuantity = New AdjustQuantity
    '    aq.Amount = Convert.ToInt32(hidAQAmount.Value)
    '    aq.BulkOrderID = RequestContext.BulkOrderID
    '    aq.AdjustedByUserID = RequestContext.CurrentUser.US_ID_PK
    '    aq.AdjustedDate = DateTime.Now
    '    aq.Comment = hidAQComment.Value
    '    aq.POID = Convert.ToInt32(hidAQPOID.Value)
    '    AdjustQuantityService.CreateAdjustQuantity(aq)
    '    Response.Redirect(Request.RawUrl)
    'End Sub

    Private Sub rptDO_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles rptDO.ItemCommand
        If e.Item.ItemType = ListItemType.AlternatingItem _
            OrElse e.Item.ItemType = ListItemType.Item Then

            Select Case e.CommandName
                Case "delete"
                    Dim hidDOId As HtmlInputHidden = CType(e.Item.FindControl("hidDOId"), HtmlInputHidden)
                    DeliveredOrderService.DeleteDeliveredOrder(Integer.Parse(hidDOId.Value))
                    Response.Redirect(Request.RawUrl)
            End Select
        End If
    End Sub

    Private Sub btnSaveAndNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveAndNext.Click
        Save()
        NavigateNext()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Save()
        Response.Redirect(Request.RawUrl)
    End Sub
End Class
